<template>

  <!-- 导出对话框 -->
  <lam-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
    <el-table ref="dataTable" v-loading="loading" :data="dataList" stripe border highlight-current-row >
      <el-table-column label="文件" align="center" prop="f" :show-overflow-tooltip="true" width="460" />
      <el-table-column label="操作" align="center" >
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-download" @click="download(scope.row)" >下载</el-button>
        </template>
      </el-table-column>
    </el-table>

    <DownloadIframe ref="downloadIframe" />

    <div slot="footer" class="dialog-footer" style="text-align: center;">
      <el-button @click="exportData" :loading="loading">重新导出</el-button>
      <el-button @click="close" :loading="loading" type="warning">关  闭</el-button>
    </div>
  </lam-dialog>

</template>

<script>
  import {httpGet} from '@/api';
  import DownloadIframe from './DownloadIframe';

  export default {
    name:'ToExport',
    components:{
      DownloadIframe
    },
    data(){
      return {
        // 遮罩层
        loading: false,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        dataList:[],
        url:'',
        params:{},
      }
    },
    methods:{
      show(title, url, params){
        this.title = title;
        this.url = url;
        this.params = params;
        this.exportData()
      },

      exportData(){
        this.loading = true;
        httpGet(this.url, this.params).then(res=>{
          this.open = true;
          if(res.data instanceof Array){
            this.dataList = res.data
          }else{
            this.dataList = [res.data]
          }
          if(this.dataList.length>0){
            this.$nextTick(()=>{
              this.download(this.dataList[0])
            })
          }
        }).finally((res)=>{
          this.loading = false;
        });
      },

      download(row){
        this.$refs.downloadIframe.download(row)
      },

      close(){
        this.title = '';
        this.url = '';
        this.open = false;
      }
    }

  }
</script>

<style scoped>

</style>
